import { _decorator, Component, Node, Vec3, v3, UITransform } from 'cc';
import { GlobalConfig } from '../../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../../Init/Managers/EventTypes';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { BasicLayer } from '../Common/Basic/BasicLayer';
import { MappingArmy } from './MappingArmy';
import { MappingObj } from './MappingObj';
import { MappingPeople } from './MappingPeople';
import { MappingTank } from './MappingTank';
const { ccclass } = _decorator;
/**3D场景中的Enemy */
@ccclass('EnemyLayer')
export class EnemyLayer extends BasicLayer {
    // #region -------------------------------层级生命周期------------
    /**初始化 只执行一次*/
    protected init() {

    };
    /**注册通过自定义事件管理器管理的事件  */
    protected onEvents() {
        this.on(EventTypes.Mapping3DEvents.SetEnemys, this.onSetEnemys, this);
    };
    /**设置状态、数据等, */
    public setData(data?: any) {

    };
    /**重置状态、数据等，子类实现 ,注销事件*/
    public reset() {
        for (const key in this._enemys) {
            const e = this._enemys[key];
            e && e.reset();
        }
        this._enemys = {};
        this._hpEnemys = {};
    }
    public customUpdate(dt: number) {
        for (const key in this._enemys) {
            const e = this._enemys[key];
            if (e && e.node.active) {
                e.update(dt);
            } else {
                e && e.reset();
                delete this._enemys[key];
            }
        }
        this.updateHP(dt);
    }
    public customLateUpdate(dt: number) {

    }
    // #endregion

    //#region ---------------------私有---------------
    private _enemys: { [uuid: string]: MappingObj } = {};
    //带血条的enemy 额外记录
    private _hpEnemys: { [uuid: string]: MappingObj } = {};
    /**预制体名称 与 BarrierTypes 对应  */
    private tmpV3 = v3();
    private tmpR = v3();
    private createEnemy(d: { enemyType: GlobalEnum.EnemyTypes, enemyName: string, node: Node, state: GlobalEnum.EnemyState }) {
        this.tmpV3.set(d.node.position).multiplyScalar(GlobalConfig.Scale2DTo3D);
        this.tmpV3.z = -this.tmpV3.y;
        this.tmpV3.y = 1;
        this.tmpR.set(d.node.eulerAngles);
        this.tmpR.y = this.tmpR.z;
        this.tmpR.z = 0;
        let e: Node;
        let _size = d.node.getComponent(UITransform).contentSize;
        let _scale = v3(_size.width, _size.width, _size.height).multiplyScalar(GlobalConfig.Scale2DTo3D);
        let hpType = 0;

        switch (d.enemyType) {
            case GlobalEnum.EnemyTypes.Army:
                e = GlobalPool.get(d.enemyName);
                e.parent = this.node;
                e.setPosition(this.tmpV3);
                e.eulerAngles = this.tmpR;
                _scale.multiplyScalar(1.1);
                e.setScale(_scale);

                this._enemys[e.uuid] = new MappingArmy(e, d);
                hpType = GlobalEnum.HpBarType.Army;
                break;
            case GlobalEnum.EnemyTypes.People:
                e = GlobalPool.get(d.enemyName);
                e.parent = this.node;
                e.setPosition(this.tmpV3);
                e.eulerAngles = this.tmpR;
                _scale.multiplyScalar(1.1);
                e.setScale(_scale);

                this._enemys[e.uuid] = new MappingPeople(e, d);
                break;
            case GlobalEnum.EnemyTypes.Tank:
                e = GlobalPool.get(d.enemyName);
                e.parent = this.node;
                e.setPosition(this.tmpV3);
                e.eulerAngles = this.tmpR;
                e.setScale(_scale);

                this._enemys[e.uuid] = new MappingTank(e, d);
                hpType = GlobalEnum.HpBarType.Tank;
                break;
            default:
                break;
        }

        let prop = this._enemys[e.uuid].mappingProps.node[GlobalEnum.ExtralProp.customProps];
        if (prop && prop.hp > 1) {
            //需要显示血条的敌人
            this._hpEnemys[e.uuid] = this._enemys[e.uuid];
            //血条信息
            GlobalTmpData.HpData[e.uuid] = {
                isShow: true, rate: prop.hp, curRate: prop.hp,
                isPlayer: false, wpos: v3(),
                countTime: 0, type: hpType,
            };
        }

        this._enemys[e.uuid].init();
    }

    /**更新Hp */
    public updateHP(dt) {
        const _cfg = GlobalTmpData.HpData;
        for (const uuid in this._hpEnemys) {
            const role = this._hpEnemys[uuid];
            if (_cfg[uuid]) {
                const prop = role.mappingProps.node[GlobalEnum.ExtralProp.customProps];
                _cfg[uuid].rate = prop.hp;
                _cfg[uuid].wpos.set(role.node.worldPosition);
                _cfg[uuid].wpos.y += 1;
            }
        }

    }

    //#endregion

    //#region ----------------------事件----------------
    /**设置碰撞物体 */
    protected onSetEnemys(d: { enemyType: GlobalEnum.EnemyTypes, enemyName: string, node: Node, state: GlobalEnum.EnemyState }[]) {
        for (let i = 0; i < d.length; i++) {
            this.createEnemy(d[i])
        }
    }

    //#endregion
}

